package com.origin.src;


import com.origin.src.aspect.DeclareParent;
import com.origin.src.aspect.DeclareTest;
import com.origin.src.aware.XAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class XApplicationContext {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(XConfiguration.class);
//        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.origin.*");

        ApplicationTool.printBean(applicationContext);

        System.out.println("====代理exposeProxy代理类中获取代理类====");
        XBeanA beanA = applicationContext.getBean(XBeanA.class);
        beanA.to(true);
        beanA.to(false);
        System.out.println("====代理@Configurable的构造方法，new出来的也注入====");
        XBeanA xBeanA = new XBeanA();
        System.out.println(xBeanA.XBeanB == beanA.XBeanB);

        System.out.println("====代理DeclareParents代理增加方法====");
        DeclareParent declareParent = (DeclareParent) applicationContext.getBean(DeclareTest.class);
        declareParent.test();

        System.out.println("====Aware不注入====");
        XBeanC xBeanC = (XBeanC) applicationContext.getBean("XBeanC");
        System.out.println("XAware的xBeanA属性：" + applicationContext.getBean(XAware.class).beanA);
        System.out.println("XAware的xAware属性：" + applicationContext.getBean(XAware.class).xAware);
        System.out.println("XBeanC的Aware属性：" + xBeanC.xAware);
        System.out.println("XBeanC的XBeanB属性：" + xBeanC.xBeanB);


        System.out.println("====代理@Configuration的proxyBeanMethods属性增强方法====");
        XConfiguration.XBeanB b1 = applicationContext.getBean(XConfiguration.class).XBeanB();
        XConfiguration.XBeanB b2 = applicationContext.getBean(XConfiguration.XBeanB.class);
        System.out.println("调用@Bean方法获取的Bean与从容器中获取的Bean是同一个：" + (b1 == b2));
        System.out.println("====@Bean注解方法重写、重载的情况====");
        Map<String, XConfiguration.XBeanB> mapB = applicationContext.getBeansOfType(XConfiguration.XBeanB.class);
        mapB.forEach((k, v) -> {
            System.out.print(k + "-->");
            System.out.println(v);
        });


    }


}
